package cn.pku.ss.fa.ui.component;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import javax.naming.InitialContext;
import org.zkoss.image.Image;
import org.zkoss.zul.Div;
import cn.pku.ss.fa.bean.AlbumBean;
import cn.pku.ss.fa.bean.AlbumNewsBean;
import cn.pku.ss.fa.bean.PhotoBean;
import cn.pku.ss.fa.sessionbean.AlbumManagerLocal;
import cn.pku.ss.fa.ui.Common;
import cn.pku.ss.fa.ui.URLStrategy;

/**
 * @author SirX
 */
public class DisplayAlbumComponent extends Div {
	
	public List<AlbumNewsBean> getAlbumNewsList(int userId) {
		try {
			InitialContext ctx = new InitialContext();
			AlbumManagerLocal albumDao = (AlbumManagerLocal)ctx.lookup("FriendsAnywhere/AlbumManager/local");
			return albumDao.getAlbumNewsList(userId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<AlbumBean> getAlbumsList(int userId) {
		try {
			InitialContext ctx = new InitialContext();
			AlbumManagerLocal albumDao = (AlbumManagerLocal)ctx.lookup("FriendsAnywhere/AlbumManager/local");
			return albumDao.getAlbumsList(userId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean createPhoto(int userId, Image img, AlbumBean albumBean) {
		String name = img.getName();
		String format = img.getFormat();
		String url = albumBean.getURL() + "/" + URLStrategy.generateRandomId() + "." + format;
		
		PhotoBean photoBean = new PhotoBean();
		photoBean.setName( name );
		photoBean.setDescription( "Click to add description" );
		photoBean.setAlbumId( albumBean.getId() );
		photoBean.setURL( url );
		
		//RPC:
		try {
			InitialContext ctx = new InitialContext();
			AlbumManagerLocal albumDao = (AlbumManagerLocal)ctx.lookup("FriendsAnywhere/AlbumManager/local");
			if( albumDao.createPhoto(userId, photoBean)==Integer.MIN_VALUE ) {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		//create image:
		String path = Common.getWebRootRealPath() + url;
		File imgFile = new File( path );
		try {
			if( imgFile.createNewFile() ) {
				OutputStream out = new BufferedOutputStream( new FileOutputStream( imgFile ) );
				out.write( img.getByteData() );
				out.flush();
				return true;
			}
			throw new Exception( "Image file: "+url+" has existed." );
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//////////////////////////////////////////////////////////////////////////////
	
//	private List<AlbumNewsBean> getAlbumNews_test() {
//		int albumNewsNumber = 5;
//		List<AlbumNewsBean> albumNewsList = new LinkedList<AlbumNewsBean>();
//		for(int i=0; i<albumNewsNumber; i++) {
//			AlbumNewsBean albumNewsBean = new AlbumNewsBean();
//			albumNewsBean.setType(
//				i%2==0 ? AlbumNewsBean.TYPE_CREATE_ALBUM : AlbumNewsBean.TYPE_UPLOAD_PHOTO
//			);
//			albumNewsBean.setUserId(i);
//			albumNewsBean.setUserName("Name"+i);
//			albumNewsBean.setAlbumName("album"+i);
//			albumNewsBean.setDate("2009-01-01");
//			albumNewsList.add(albumNewsBean);
//		}
//		return albumNewsList;
//	}
	
//	private List<AlbumBean> getAlbums_test() {
//	int albumNumber = new Random().nextInt(5);
//	int photoNumber = new Random().nextInt(10);
//	List<AlbumBean> albumList = new LinkedList<AlbumBean>();
//	for(int i=0; i<albumNumber; i++) {
//		AlbumBean albumBean = new AlbumBean();
//		List<PhotoBean> photoList = new LinkedList<PhotoBean>();
//		for(int j=0; j<photoNumber; j++) {
//			PhotoBean photoBean = new PhotoBean();
//			String pid = Integer.toString(i)+Integer.toString(j);
//			photoBean.setId( Integer.parseInt(pid) );
//			photoBean.setName("name"+pid);
//			photoBean.setDescription( "Description"+pid );
//			photoBean.setURL("/user/0/album/0/0.jpg");
//			photoList.add(photoBean);
//		}
//		albumBean.setId(i);
//		albumBean.setName("Name"+i);
//		albumBean.setDescription("Description"+i);
//		albumBean.setPhotos(photoList);
//		albumBean.setUserId(0);
//		albumBean.setURL("/user/0/album/0");
//		albumList.add(albumBean);
//	}
//	return albumList;
//}

}
